package com.gsafety.libraryktx.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.gsafety.libraryktx.R
import com.scwang.smart.refresh.layout.SmartRefreshLayout

/**刷新
 * @author : lj
 * @date : 2020/7/20 16:49
 */
class CommonRefreshView@JvmOverloads constructor(context: Context,
                                                 attrs: AttributeSet? = null,
                                                 defStyleAttr: Int = 0) : LinearLayout(context,attrs,defStyleAttr),EmptyLayout.OnRetryListener{
     var mEmptyLayout: EmptyLayout? = null

     var mSmartRefreshLayout: SmartRefreshLayout? = null


     var mRefreshListener: RefreshListener? = null
    init {
        initview(attrs)
    }


    private fun initview(attrs:AttributeSet?){
        var view = LayoutInflater.from(context).inflate(R.layout.common_refresh_layout, this, false)
        var typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.common_RefreshLayout)
        var layout = typedArray.getResourceId(R.styleable.common_RefreshLayout_common_layout,0)
        if(layout!=0) {
            LayoutInflater.from(context).inflate(layout, view.findViewById<FrameLayout>(R.id.base_content), true)
        }
        mEmptyLayout = view.findViewById(R.id.empty_layout)
        mSmartRefreshLayout = view.findViewById(R.id.srl_msg)
        addView(view)
        initSwipeRefresh()
    }


    /**
     * 初始化下拉刷新
     */
    private fun initSwipeRefresh() {
        if (mSmartRefreshLayout != null) {
            //设置下拉刷新和上拉加载
            mSmartRefreshLayout!!.setDisableContentWhenRefresh(true)
            mSmartRefreshLayout!!.setOnRefreshListener {
                mRefreshListener?.let {
                    it.onRefresh()
                }
            }
            mSmartRefreshLayout!!.setOnLoadMoreListener {
                mRefreshListener?.let {
                    it.onLoadMore()
                }
            }
        }
    }
    fun showNetError() {
        mEmptyLayout?.let {
            it.setEmptyStatus(EmptyLayout.STATUS_NO_NET)
            it.setRetryListener(this)
        }
    }

    fun finishRefresh() {
        mSmartRefreshLayout?.let {
            it.finishRefresh(0)
            it.finishLoadMore(0)
        }
    }

    /**
     * 否启用上拉加载更多
     */
    fun setEnableLoadMore(enable: Boolean) {
        mSmartRefreshLayout?.let {
            it.setEnableLoadMore(enable)
        }
    }

    fun loadData() {
        mEmptyLayout?.let {
            it.setEmptyStatus(EmptyLayout.STATUS_HIDE)
        }
    }

    fun loadNoData() {
        mEmptyLayout?.let {
            it.setEmptyStatus(EmptyLayout.STATUS_NO_DATA)
            mEmptyLayout!!.setRetryListener(this)
        }
    }

    fun loadErrorData() {
        mEmptyLayout?.let {
            it.setEmptyStatus(EmptyLayout.STATUS_NO_DATA)
            mEmptyLayout!!.setRetryListener(this)
        }
        finishRefresh()
    }

    fun loadMoreData() {
        mSmartRefreshLayout?.let {
            it.finishLoadMore(0)
        }
    }

    /**
     * 设置刷新监听器
     * @param retryListener 监听器
     */
    fun setRrefreshListener(refreshListener:RefreshListener?) {
        mRefreshListener = refreshListener
    }

   fun autoRefresh(){
       mSmartRefreshLayout?.let {
           it.autoRefresh()
       }
   }

    fun isRefreshing():Boolean{
        return mSmartRefreshLayout!!.isRefreshing
    }

    fun isLoading():Boolean{
        return mSmartRefreshLayout!!.isLoading
    }
    /**
     * 点击重试监听器
     */
    interface RefreshListener {
        fun onRefresh()
        fun onLoadMore()
    }

    override fun onRetry() {
        autoRefresh()
    }

}